#!/bin/bash
function setup_phoenix() {
  echo_start "$1 ... ..."
  confirm_operate "install phoenix"
  if [ $? -eq 0 ]; then
    if [ "x$HRS_PHOENIX_HOST" = "x" ]; then
      echo_warn "The variable HRS_PHOENIX_HOST does not assign a value, Unable to install! "
      echo_passed "$1 SKIP"
      return 0
    fi
    echo_doing "deliver media ... ..."
    # 清理上次传输的安装包，并重新传输
    ssh -o StrictHostKeyChecking=no $HRS_PHOENIX_HOST "if [ -f $HYRENTMP_HOME/phoenix-4.8.0-cdh5.8.0.tar.gz ]; then rm -rf $HYRENTMP_HOME/phoenix-4.8.0-cdh5.8.0.tar.gz; fi"  || { die "clean pkg dir fail"; }
    ssh -o StrictHostKeyChecking=no $HRS_PHOENIX_HOST "if [ -d $HYRENTMP_HOME/phoenix-4.8.0-cdh5.8.0 ]; then rm -rf $HYRENTMP_HOME/phoenix-4.8.0-cdh5.8.0; fi" || { die "clean pkg fail"; }
    scp $XDIR/packages/phoenix-4.8.0-cdh5.8.0.tar.gz $HRS_PHOENIX_HOST:$HYRENTMP_HOME >> $SETUP_LOGFILE || { die "deliver media fail"; }
    echo_done

    echo_doing "install soft ... ..."
    # 建目录，并解压安装
    ssh -o StrictHostKeyChecking=no $HRS_PHOENIX_HOST "if [ -d $HRS_PHOENIX_HOME ]; then rm -rf $HRS_PHOENIX_HOME/*; fi" || { die "del fail : $HRS_PHOENIX_HOME/*"; }
    ssh -o StrictHostKeyChecking=no $HRS_PHOENIX_HOST "if [ ! -d $HRS_PHOENIX_HOME ]; then mkdir -p $HRS_PHOENIX_HOME; fi" || { die "make phoenix_dir fail : $HRS_PHOENIX_HOME"; }

    ssh -o StrictHostKeyChecking=no $HRS_PHOENIX_HOST "tar zxf $HYRENTMP_HOME/phoenix-4.8.0-cdh5.8.0.tar.gz -C \"$HYRENTMP_HOME\"" ||{ die "phoenix decompressed fail"; }
    #ssh -o StrictHostKeyChecking=no $HRS_PHOENIX_HOST "mkdir -p $HRS_PHOENIX_HOME" || { die "make phoenix_dir fail!"; }
    ssh -o StrictHostKeyChecking=no $HRS_PHOENIX_HOST "mv $HYRENTMP_HOME/phoenix-4.8.0-cdh5.8.0/* $HRS_PHOENIX_HOME" ||{ die "mv files fail : move [$HYRENTMP_HOME/phoenix-4.8.0-cdh5.8.0/*] TO [$HRS_PHOENIX_HOME]"; }
    echo_done

    echo_doing "deliver phoenix jar to CHD_HOSTS ... ..."
    scp $HRS_PHOENIX_HOST:$HRS_PHOENIX_HOME/phoenix-4.8.0-cdh5.8.0-server.jar $HYRENTMP_HOME >> $SETUP_LOGFILE || { die "scp fail : from [$HRS_PHOENIX_HOST:$HRS_PHOENIX_HOME/phoenix-4.8.0-cdh5.8.0-server.jar] TO [$HYRENTMP_HOME]"; }
    local value_arr=($CDH_HOSTS)
    for i in ${value_arr[@]}; do
      scp $HYRENTMP_HOME/phoenix-4.8.0-cdh5.8.0-server.jar  $i:$HADOOP_CLUSTER_HOME/lib/hbase/lib >> $SETUP_LOGFILE || { die "scp fail : from [$HYRENTMP_HOME/phoenix-4.8.0-cdh5.8.0-server.jar] TO [$i:$HADOOP_CLUSTER_HOME/lib/hbase/lib]"; }
    done
    echo_done
    echo_error "目前只是在 PHOENIX 主机上完成了解压，并没有启动服务，那么，为什么还需要有这么一台机器？"

    echo_info "set profile on all hyren hosts ... ..."
    # 给所有主机，设置phoenix的环境变量
    local value_arr=($HYREN_ALLHOSTS)
    for i in ${value_arr[@]}; do
      echo_doing "  host : $i >>>>>> "
      ssh -o StrictHostKeyChecking=no $i 'bash -s' < $XDIR/util/func_rmot_setprofile.sh "HRS_PHOENIX_HOME" "$HRS_PHOENIX_HOME" "$HYREN_USER"  || { die "set HRS_PHOENIX_HOME fail on $i"; }
      echo_done
    done
    echo_done

    echo_success "$1"
    echo_info "****************************************"
    echo_info "PHONIX HOST    dir  : $HRS_PHOENIX_HOME"
    echo_info "****************************************"

    # 组装zk主机列表
    local value_zkhosts=($HRS_ZOOKEEPER_HOSTS)
    local zkhosts=""
    for i in ${value_zkhosts[@]}; do
      zkhosts=$zkhosts,$i:$HRS_ZOOKEEPER_clientPort 
    done
    zkhosts=${zkhosts:1}
    echo
    echo_warn "========================================"
    echo_warn " 请按照如下步骤验证服务："
    echo_warn " 1. 使用用户：$HYREN_USER，登录主机：$HRS_PHOENIX_HOST"
    echo_warn " 2. 进入目录：$HRS_PHOENIX_HOME/bin"
    echo_warn " 3. 执行：./sqlline.py $zkhosts"
    echo_warn " 4. 在当前命令界面中输入：!tables 验证正确后，退出： !q"
    echo_warn "========================================="

    echo_passed "$1 done."
  else
    echo_passed "$1 SKIP"
  fi
}